window.onload = () => {
    getData();
    setInterval(() => {
        getData();
    },3000)
}

function getData() {
    function getMemoryData() {
        return axios.get('/data/memory');
    }
    function getStaticData() {
        return axios.get('/data/static');
    }
    function getErrorData() {
        return axios.get('/data/error');
    }
    function getXHRData() {
        return axios.get('/data/xhr');
    }
    axios.all([getMemoryData(), getStaticData(), getErrorData(), getXHRData()])
        .then((result) => {
            result.forEach(item => {
                setdata(item.data);
            });
        }).catch((err) => {
            console.log(err);
        });
}

function setdata(data) {
    let state = document.querySelector('#state');
    let usedJSHeapSize = document.querySelector('#usedJSHeapSize');
    let totalJSHeapSize = document.querySelector('#totalJSHeapSize');
    let jsHeapSizeLimit = document.querySelector('#jsHeapSizeLimit');

    let scriptEl = document.querySelector('#script');
    let imgEl = document.querySelector('#img');
    let linkEl = document.querySelector('#link');

    let errorCount = document.querySelector('#errorCount');

    let xhrerr = document.querySelector('#XHR-ERR');
    let xhrok = document.querySelector('#XHR-OK');
    let title  = document.querySelector('#date');

    title.textContent = data.data[6].day

    let number = data.data[6].data.length;
    let temp = data.data[6].data;
    switch (data.type) {
        case 'memory':
            temp.sort((a, b) => {
                return fromartDate(a.createTime) > fromartDate(b.createTime) ? -1 : 1;
            });
            state.textContent = temp[0].state
            usedJSHeapSize.textContent = `${temp[0].usedJSHeapSize}Mb`
            totalJSHeapSize.textContent = `${temp[0].totalJSHeapSize}Mb`
            jsHeapSizeLimit.textContent = `${temp[0].jsHeapSizeLimit}Mb`
            if (temp[0].state != '正常') {
                state.classList.remove('badge-success')
                state.classList.add('badge-danger')
            }
            break;
        case 'static':
            let linkCount = 0;
            let srciptCount = 0;
            let imageCount = 0;
            temp.forEach(item => {
                if (item.el === 'link') linkCount++;
                if (item.el === 'img') imageCount++;
                if (item.el === 'script') srciptCount++;
            })
            imgEl.textContent = imageCount;

            if (imageCount > 0) {
                imgEl.classList.remove('badge-success')
                imgEl.classList.add('badge-danger')
            }
            if (srciptCount > 0) {
                scriptEl.classList.remove('badge-success')
                scriptEl.classList.add('badge-danger')
            }
            if (linkCount > 0) {
                linkEl.classList.remove('badge-success')
                linkEl.classList.add('badge-danger')
            }
            linkEl.textContent = linkCount;
            scriptEl.textContent = srciptCount;
            break;
        case 'error':
            errorCount.innerText = `${number}`
            if (number > 0) {
                errorCount.classList.remove('badge-success')
                errorCount.classList.add('badge-danger')
            }
            break;
        case 'xhr':
            let okCount = 0;
            temp.forEach(item => {
                if (item.status >= 200 && item.status <= 301) okCount++;
            })
            xhrok.textContent = okCount;
            xhrerr.textContent = number - okCount;

            if ((number - okCount) > 0) {
                xhrerr.classList.remove('badge-success')
                xhrerr.classList.add('badge-danger')
            }
            break;
        default:
            break;
    }
}
function fromartDate(date) {
    return new Date(date.replace(/-/g, '/')).getTime();
}